package com.pjmike.client.proxy;

import com.pjmike.client.netty.NettyClient;
import com.pjmike.common.protocol.RpcRequest;
import com.pjmike.common.protocol.RpcResponse;
import com.pjmike.common.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @description:
 * @author: pjmike
 * @create: 2019/04/07 16:53
 */

public class RpcClientDynamicProxy<T> implements InvocationHandler {

    private static final Logger log= LoggerFactory.getLogger(RpcClientDynamicProxy.class);

    private Class<T> clazz;
//    private ServiceDiscover discover = new ZkServiceDiscover("127.0.0.1:2181");
    public RpcClientDynamicProxy(Class<T> clazz) throws Exception {
        this.clazz = clazz;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {

        log.info("method对象表示的方法的类的class对象={}",method.getDeclaringClass());
        log.info("Object.class={}",Object.class);
        if(Object.class.equals(method.getDeclaringClass())){
            log.info("调用代理={}",this);
            log.info("参数={}", JsonUtil.toJson(args));
            return method.invoke(this,args);
        }else{
            RpcRequest request = new RpcRequest();
            String requestId = UUID.randomUUID().toString();

            String className = method.getDeclaringClass().getName();
            String methodName = method.getName();

            Class<?>[] parameterTypes = method.getParameterTypes();

            request.setRequestId(requestId);
            request.setClassName(className);
            request.setMethodName(methodName);
            request.setParameterTypes(parameterTypes);
            request.setParameters(args);
            log.info("请求内容: {}",request);

//        String address = discover.discover();
//        String[] arrays = address.split(":");
//        String host = arrays[0];
//        int port = Integer.parseInt(arrays[1]);
            NettyClient nettyClient = new NettyClient("127.0.0.1", 8888);
            log.info("开始连接服务端：{}",new SimpleDateFormat("yyyy-hh-MM hh:mm:ss").format(new Date()));
           nettyClient.connect();
            RpcResponse send = nettyClient.send(request);
            log.info("请求调用返回结果：{}", send.getResult());
            return send.getResult();
        }
    }
}
